package com.smartens.boit.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.smartens.boit.R;
import com.smartens.boit.activity.AccountActivity;
import com.smartens.boit.activity.FeedbackActivity;
import com.smartens.boit.activity.SaveActivity;
import com.smartens.boit.login.LoginActivity;

import java.util.Objects;

/**
 * @Description 我的
 * @Author songtt
 * @Date 2021/3/4
 */
public class FourthFragment extends Fragment {
    private LinearLayout account;
    /**
     * 点击账号安全
     */
    private LinearLayout logout;
    /**
     * 点击关于我们
     */
    private LinearLayout about;
    /**
     * 点击意见反馈
     */
    private LinearLayout feedback;
    /**
     * 确定按钮
     */
    private TextView outConfirm;
    /**
     * 取消按钮
     */
    private TextView outCancel;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_fourth, container, false);

        account = view.findViewById(R.id.lin_safe);
        logout = view.findViewById(R.id.logout);
        // 获取【关于我们】的linerLayout
        about = view.findViewById(R.id.lin_about);
        // 获取【意见反馈】的linerLayout
        feedback = view.findViewById(R.id.lin_feedback);
        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 点击退出登录 显示弹框
                View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_out,null,false);
                AlertDialog dialog = new AlertDialog.Builder(getActivity()).setView(view).create();
                // dialog 退出确认按钮
                outConfirm = view.findViewById(R.id.out_confirm);
                // 打开dialog
                dialog.show();
                // 设置dialog背景颜色为透明
                dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

                // 关闭对话框
                outCancel = view.findViewById(R.id.out_cancel);
                outCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.cancel();
                }
            });

                // 设置dialog 的宽高
                WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
//                params.width = dip2px(FourthFragment.this.getActivity(),270);
//                params.height = dip2px(FourthFragment.this.getActivity(),170);
                dialog.getWindow().setAttributes(params);
                outConfirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                        Objects.requireNonNull(getActivity()).finish();
                    }
                });
            }
        });

        /**
         * 点击账号安全
         */
        account.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AccountActivity.class);
                startActivity(intent);
            }
        });

        /** 点击关于我们，进行页面跳转 */
        about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SaveActivity.class);
                startActivity(intent);
                /** Objects.requireNonNull(getActivity()).finish() */
                /** 注意！！！ 不要关闭该页面，之后还得跳转回来 */

            }
        });

        /**
         * 点击意见反馈，进行页面跳转
         */

        feedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), FeedbackActivity.class);
                startActivity(intent);
                /** Objects.requireNonNull(getActivity()).finish() */
                /** 注意！！！ 不要关闭该页面，之后还得跳转回来 */
            }
        });
        return view;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);

        SharedPreferences sharedPreferences=context.getSharedPreferences("data", Context .MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.apply();
    }
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}